package com.yxr.affairs.api;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.yxr.affairs.MyApplication;
import com.yxr.affairs.utils.UserInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ServerApi implements Constants{

    /**
     * 公众号
     */
    public static final String weChatPublicNumber = isKccf ? "kuaichecf" : "cthaochedai";
    /**
     * 微博
     */
    public static final String sinaUrl = isKccf ? "http://weibo.com/kuaichecaifu?sudaref=www.kuaichecaifu.com&is_hot=1" :
            "http://weibo.com/haochedai?sudaref=www.haochedai.com&is_hot=1";

    private static String versionName = null;

    public static void init(Context context) {
        versionName = getVersionName(context);
    }

    public static List<String> getParameterList(){
        List<String> list = new ArrayList<>();

        //所有接口必传参数：渠道号 来源 版本号
        String channel_id = MyApplication.channel_id;

        list.add(channel_id);
        list.add(agent);
        list.add(versionName);
        list.add(versionName);

        return list;
    }

    public static Map<String, Object> getParameter(){
        HashMap<String, Object> map = new HashMap<>();

        //所有接口必传参数：渠道号 来源 版本号
        String channel_id = MyApplication.channel_id;
        if (channel_id != null) {
            map.put("qbsource", channel_id);
        }

        map.put("agent", agent);

        if (versionName != null) {
            map.put("version_name", versionName);
            map.put("app_version", versionName);
        }

        return map;
    }

    private static String getVersionName(Context mContext) {
        PackageManager packageManager = mContext.getPackageManager();
        try {
            PackageInfo packInfo = packageManager.getPackageInfo(mContext.getPackageName(), 0);
            String version = packInfo.versionName;
            return version;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * app跳转wap站
     *
     * @param url
     * @return
     */
    public static String redirectURL(String url){
        StringBuffer sb = new StringBuffer();

        if (url.contains("?")) {
            sb.append("&agent=");
        }else {
            sb.append("?agent=");
        }
        sb.append(agent);

        if (UserInfo.isLogin()) {
            sb.append("&sessionid=");
            sb.append(UserInfo.getSessionid());
        }

        String channel_id = MyApplication.channel_id;
        if (channel_id != null) {
            sb.append("&qbsource=");
            sb.append(channel_id);
        }

        if (versionName != null) {
            sb.append("&version_name=");
            sb.append(versionName);

            sb.append("&app_version=");
            sb.append(versionName);
        }

        sb.append("&from_app=1");

        return url + sb.toString();
    }

}
